<template>
  <a-modal
    :title="title"
    :width="width"
    :bodyStyle="bodyStyle"
    style="top: 0px;padding: 0;"
    :visible="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    <template slot="footer">
      <a-button key="back" @click="handleCancel">关闭</a-button>
      <a-button key="submit" type="primary" @click="handleOk">更新</a-button>
    </template>

    <a-spin :spinning="confirmLoading">
      <a-form :form="form" class="body-form">
        <div class='headerTitle' v-if="model.processName">{{ model.processName }}</div>
        <a-row :gutter="24">
          <a-col :span="24">
            <a-textarea v-model="contentNote" :autosize="{ minRows: 2 }"></a-textarea>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>

import {getAction, postAction} from '@/api/manage'

export default {
  name: 'ExtActProcessUpdateModal',
  data() {
    return {
      form: this.$form.createForm(this),
      title: '操作',
      width: document.body.clientWidth,
      bodyStyle: {
        padding: "0",
        top: "0",
        height: (window.innerHeight - 110) + "px",
        "overflow-y": "auto"
      },
      visible: false,
      model: {},
      confirmLoading: false,
      url: {
        getProcessXml: '/designer/mock/getProcessXml',          //获取流程图文件接口
        updateProcessXml: '/designer/mock/updateProcessDescriptor',  //更新流程图文件接口
      },
      contentNote: '',   //流程文件
    }
  },
  created() {
  },
  methods: {
    edit(record) {
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true
      this.$nextTick(() => {
        this.getData(record)
      })
    },
    getData(record) {
      if (record.id) {
        var param = {processId: record.id}
        this.confirmLoading = true
        getAction(this.url.getProcessXml, param).then((res) => {
          this.contentNote = res ? res : ''
        }).finally(() => {
          this.confirmLoading = false
        })
      }
    },
    close() {
      this.$emit('close')
      this.visible = false
      this.model = {}
      this.contentNote = ''
      this.bodyStyle = {
        padding: '0',
        top: '0',
        height: (window.innerHeight - 110) + 'px',
        'overflow-y': 'auto'
      }
      document.getElementsByClassName('ant-modal-body')[0].scrollTop = 0
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          if (!that.contentNote) {
            that.$message.destroy()
            that.$message.warning('请填写流程文件!')
            return
          }
          var formData = {
            id: this.model.id,
            note: this.contentNote
          }
          // console.log(formData)
          // return
          that.confirmLoading = true
          postAction(that.url.updateProcessXml, formData).then((res) => {
            if (res.success) {
              that.$message.success(res.msg)
              that.$emit('ok')
            } else {
              that.$message.warning(res.msg)
            }
          }).finally(() => {
            that.confirmLoading = false
            that.close()
          })
        }
      })
    },
    handleCancel() {
      this.close()
    }
  }
}
</script>
<style scoped lang="less">
.body-form {
  width: calc(100% - 25px);
  margin: 20px auto;
}

.headerTitle {
  margin-bottom: 10px;
  font-weight: bold;
  text-align: center;
  display: block;
  color: black;
  font-size: 24px;
}
</style>